#!/bin/bash

# 脚本名称: modify_topas_histories.sh
# 功能: 批量修改dedx_m1到dedx_m25目录下carbon.txt文件中的NumberOfHistoriesInRun参数
# 作者: Claude
# 日期: $(date +%Y-%m-%d)

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 计数器
success_count=0
error_count=0
total_files=0

echo -e "${BLUE}===================================================${NC}"
echo -e "${BLUE}  TOPAS 参数批量修改脚本${NC}"
echo -e "${BLUE}===================================================${NC}"
echo -e "${YELLOW}任务: 修改 NumberOfHistoriesInRun 从 10000 到 500000${NC}"
echo -e "${YELLOW}目标目录: dedx_m1 到 dedx_m25${NC}"
echo -e "${YELLOW}目标文件: carbon.txt${NC}"
echo ""

# 检查当前目录是否存在目标子目录
if [ ! -d "dedx_m1" ]; then
    echo -e "${RED}错误: 当前目录下未找到 dedx_m1 目录${NC}"
    echo -e "${YELLOW}请确保在包含 dedx_m1 到 dedx_m25 目录的父目录中运行此脚本${NC}"
    exit 1
fi

# 创建备份目录
backup_dir="backup_$(date +%Y%m%d_%H%M%S)"
echo -e "${BLUE}创建备份目录: ${backup_dir}${NC}"
mkdir -p "$backup_dir"

# 遍历dedx_m1到dedx_m25目录
for i in {1..25}; do
    dir_name="dedx_m${i}"
    carbon_file="${dir_name}/carbon.txt"
    
    echo -e "\n${BLUE}处理目录: ${dir_name}${NC}"
    
    # 检查目录是否存在
    if [ ! -d "$dir_name" ]; then
        echo -e "${YELLOW}  警告: 目录 ${dir_name} 不存在，跳过${NC}"
        continue
    fi
    
    # 检查carbon.txt文件是否存在
    if [ ! -f "$carbon_file" ]; then
        echo -e "${YELLOW}  警告: 文件 ${carbon_file} 不存在，跳过${NC}"
        continue
    fi
    
    total_files=$((total_files + 1))
    
    # 创建备份
    backup_file="${backup_dir}/${dir_name}_carbon.txt"
    cp "$carbon_file" "$backup_file"
    echo -e "${GREEN}  ✓ 已备份到: ${backup_file}${NC}"
    
    # 检查文件中是否包含目标参数
    if grep -q "i:So/Demo/NumberOfHistoriesInRun.*=.*500000" "$carbon_file"; then
        # 使用sed进行替换
        if sed -i 's/i:So\/Demo\/NumberOfHistoriesInRun[[:space:]]*=[[:space:]]*500000/i:So\/Demo\/NumberOfHistoriesInRun = 5000000/g' "$carbon_file"; then
            echo -e "${GREEN}  ✓ 成功修改: ${carbon_file}${NC}"
            echo -e "${GREEN}    500000 → 5000000${NC}"
            success_count=$((success_count + 1))
        else
            echo -e "${RED}  ✗ 修改失败: ${carbon_file}${NC}"
            error_count=$((error_count + 1))
        fi
    else
        echo -e "${YELLOW}  ⚠ 未找到匹配的参数行，检查文件内容${NC}"
        # 显示可能的匹配行
        grep -n "NumberOfHistoriesInRun" "$carbon_file" | head -3
        error_count=$((error_count + 1))
    fi
done

# 显示总结
echo -e "\n${BLUE}===================================================${NC}"
echo -e "${BLUE}  修改完成统计${NC}"
echo -e "${BLUE}===================================================${NC}"
echo -e "${GREEN}成功修改: ${success_count} 个文件${NC}"
echo -e "${RED}失败/跳过: ${error_count} 个文件${NC}"
echo -e "${BLUE}总计处理: ${total_files} 个文件${NC}"
echo -e "${BLUE}备份位置: ${backup_dir}${NC}"

# 提供验证命令
echo -e "\n${YELLOW}验证修改结果的命令:${NC}"
echo -e "${YELLOW}grep -r \"NumberOfHistoriesInRun\" dedx_m*/carbon.txt${NC}"

# 如果有错误，提供恢复命令
if [ $error_count -gt 0 ]; then
    echo -e "\n${YELLOW}如需恢复备份:${NC}"
    echo -e "${YELLOW}for i in {1..25}; do [ -f ${backup_dir}/dedx_m\${i}_carbon.txt ] && cp ${backup_dir}/dedx_m\${i}_carbon.txt dedx_m\${i}/carbon.txt; done${NC}"
fi

echo -e "\n${GREEN}脚本执行完成！${NC}"
